<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

$query = "SELECT gold FROM ruler WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$gold = $row[ 'gold' ];
$query = "SELECT * FROM army WHERE army_id = " . formatInput( $_POST[ 'army_id' ] );
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
$status = "";
$mission_type = -1;
if( isset( $_POST[ 'mission_radio' ] ) ) {
	$mission_type = $_POST[ 'mission_radio' ];
}
//for( $i = 0; $i < 3; $i ++ ) {
//	if( isset( $_POST[ 'mission_type' . $i ] ) ) {
//		$mission_type = $i;
//	}
//}
//if( !isset( $mission_type ) ) {
//	$mission_type = -1;
//}
if( $mission_type == 0 && $row[ 'fortified' ] == 0 ) {
	$mission_cost = 20;
	$mission_time = 5;
}
else if( $mission_type == 1 && $row[ 'fortified' ] == 0 ) {
	$mission_cost = 40;
	$mission_time = 10;
}
else if( $mission_type == 2 && $row[ 'fortified' ] == 0 ) {
	$mission_cost = 80;
	$mission_time = 15;
}
else if( $mission_type == 3 && $row[ 'fortified' ] == 0 ) {
	$mission_cost = 160;
	$mission_time = 20;
}
else if( $mission_type == 4 && $row[ 'fortified' ] == 0 ) { // fortify mission
	$mission_cost = 200;
	$mission_time = 8;
}
else if( $mission_type == 5 && $row[ 'fortified' ] == 1 ) { // dig out of fortified position
	$mission_cost = 80;
	$mission_time = 4;
}

if( $mission_type == -1 ) {
	$status = "No mission selected.";
}
elseif( $row[ 'training' ] > 0 ) {
	$status = "Can't start mission, army is in training.";
}
elseif( $row[ 'travel' ] > 0 ) {
	$status = "Can't start mission, army is traveling.";
}
elseif( $row[ 'ruler_id' ] != $_SESSION[ 'ruler_id' ] ) {
	$status = "You don't own this army!'";
}
elseif( $row[ 'mission' ] > 0 ) {
	$status = "Can't start mission, army is already on a mission.";
}
elseif( $row[ 'follow' ] > 0 ) {
	$status = "Can't start mission the army is following another army.";
}
elseif( $row[ 'scout' ] == 0 && $mission_type < 4 ) {
	$status = "Can't start this mission, army has no scouts.";
}
elseif( $gold < $mission_cost ) {
	$status = "Can't start this mission, you don't have enough gold.";
}
else {
	$query = "UPDATE ruler SET gold = " . ( $gold - $mission_cost ) . " WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$query = "UPDATE army SET mission = " . $mission_time . ", mission_type = " . $mission_type . " WHERE army_id = " . $_POST[ 'army_id' ];
	mysql_query( $query ) or die( "Query failed : " . mysql_error() );
}
if( $status != "" ) {
	$_SESSION[ 'start_mission2_status' ] = translate( $status );
}
header( "Location: operations.php" );
?>